package com.example.zingalbumdownloader.utility;

import java.io.IOException;
import java.util.ArrayList;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import com.example.zingalbumdownloader.model.Song;

public class SongsFinder {
	public static String SONG_TITLE = "title";
	public static String SONG_PERFORMER = "performer";
	public static String SONG_SOURCE = "source";

	// public SongsFinder(URL url, ArrayList<Song> songs) throws ParseException
	// {
	// findSongs(url, songs);
	// }

	public static void findSongs(String url, ArrayList<Song> songs)
			throws ParseException, IOException {
		JSONParser jsonParser = new JSONParser();
		JSONObject root = (JSONObject) jsonParser.parse(StringUtils
				.getStringFromURLString(url));
		JSONArray songArray = (JSONArray) root.get("data");

		for (int i = 0; i < songArray.size(); ++i) {
			Song song = new Song();
			JSONObject songObject = (JSONObject) songArray.get(i);
			song.setTitle((String) songObject.get(SONG_TITLE));
			song.setPerformer((String) songObject.get(SONG_PERFORMER));
			song.setSource((String) songObject.get(SONG_SOURCE));

			songs.add(song);
		}
	}
}
